package sjp.demo.jfinal.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import sjp.demo.jfinal.dto.Product;
import sjp.demo.jfinal.interceptor.AaaInter;
import sjp.demo.jfinal.interceptor.BbbInter;
import sjp.demo.jfinal.interceptor.DemoIntercepter;
import sjp.demo.jfinal.model.Reply;

import java.util.Arrays;
import java.util.List;

/**
 * Created by vcc on 2017/3/6.
 */
@Before(DemoIntercepter.class)
public class HelloController extends Controller {

    @Before(AaaInter.class)
    public void index() {
        renderText("Hello JFinal World");
    }

    @Before(BbbInter.class)
    public void test() {
        //Controller提供了getPara系列方法用来从请求中获取参数


        //Blog blog = getModel(Blog.class);
        setAttr("text1", "Hello");
        setAttr("text2", "World");
        //setAttr("blog", blog);
        renderText("此方法是一个action");
    }

    public void free1() {
        setAttr("user", "Shan Junpeng1");
        setAttr("latestProduct", new Product("products/greenmouse.html", "green mouse"));

        List<Product> products = Arrays.asList(new Product("products/greenmouse.html", "green mouse"),
                new Product("products/redhorse.html", "red horse"),
                new Product("products/blackbird.html", "black bird"));

        setAttr("products", products);
        render("/views/demo/free1.ftl");
    }

    public void free2() {
        List<Reply> replys = Reply.dao.find("select * from reply");
        setAttr("replys", replys);

        Reply reply = Reply.dao.findById(439171823129539L);
        setAttr("reply", reply);

        render("/views/demo/free2.ftl");
    }


    /**
     * 直接确定值
     */
    public void free333() {
        render("/views/demo/free3_3_3.ftl");
    }

    /**
     * 序列操作
     */
    public void free336() {
        setAttr("boys", Arrays.asList("Jim", "Tom", "Black", "Harry"));
        setAttr("seasons", Arrays.asList("spring", "summer", "autumn", "winter"));
        setAttr("girls", Arrays.asList("Lily", "Li Mei", "Xuxu"));
        setAttr("hello", "hello");
        setAttr("world", "world");
        render("/views/demo/free3_3_6.ftl");
    }

    public void free3311() {
        setAttr("html1", "<div><p>hello world<p></div>");
        render("/views/demo/free3_3_11.ftl");
    }

    /**
     * 宏
     */
    public void free41() {
        render("/views/demo/free4_1.ftl");
    }

    public void free42() {
        render("/views/demo/free4_2.ftl");
    }
    public void free43() {
        render("/views/demo/free4_3.ftl");
    }

    public void uploadFile() {
        /**
         * Controller 提供了 getFile 系列方法支持文件上传。 特别注意：如果客户端请求为 multipart
         request（form 表单使用了 enctype="multipart/form-data"），那么必须先调用 getFile 系列方法才
         能使 getPara 系列方法正常工作，因为 multipart request 需要通过 getFile 系列方法解析请求体中
         的数据，包括参数。同样的道理在 Interceptor、Validator 中也需要先调用 getFile。
         */
    }
}
